package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@DubboService
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Example example = new Example(Permission.class);
        Example.Criteria criteria = example.createCriteria();
        if (queryPageBean.getQueryString() != null) {
            criteria.orLike("name", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("description", "%" + queryPageBean.getQueryString() + "%");
            criteria.orLike("keyword", "%" + queryPageBean.getQueryString() + "%");
        }
        Page<Permission> page = (Page<Permission>) permissionMapper.selectByExample(example);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Permission findById(Integer id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public void edit(Permission permission) {
        int i = permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public void delete(Integer id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void add(Permission permission) {
        permissionMapper.insertSelective(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionMapper.selectAll();
    }
}
